package cn.edu.fzu.homemaking.redis;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.support.spring.FastJsonRedisSerializer;

/**
 * redis操作
 *
 * @author fangxin
 * @date 2022/11/8
 */
@Component
public class RedisClient extends RedisTemplate<String, Object> {

    public RedisClient() {
        RedisSerializer<Object[]> serializer = new FastJsonRedisSerializer<>(Object[].class);
        RedisSerializer<String> stringSerializer = new StringRedisSerializer();
        this.setKeySerializer(stringSerializer);
        this.setValueSerializer(serializer);
        this.setHashKeySerializer(stringSerializer);
        this.setHashValueSerializer(serializer);
    }

    @Autowired
    public RedisClient(RedisConnectionFactory connectionFactory) {
        this();
        this.setConnectionFactory(connectionFactory);
        this.afterPropertiesSet();
    }
}
